<?php

namespace common\models;

use builder\behaviors\DatetimeBehavior;
use Yii;

/**
 * This is the model class for table "{{%app_logs}}".
 *
 * @property int $id
 * @property string $level 日志等级, DEBUG、INFO、WARNING、ERROR
 * @property string $category 种类, 例: 订单支付
 * @property string|null $prefix 客户端
 * @property string|null $message 信息
 * @property string $business_type 业务类型
 * @property int $business_id 业务ID
 * @property string $created_at 记录时间
 */
class AppLogs extends \builder\base\BaseActiveRecord
{
    // 级别映射标签名
    const LEVEL_LABELS_MAP = ['DEBUG' => '调试', 'INFO' => '信息', 'WARNING' => '警告', 'ERROR' => '错误'];

    // 级别映射标签色
    const LEVEL_COLORS_MAP = ['DEBUG' => 'gray1', 'INFO' => 'success', 'WARNING' => 'warning', 'ERROR' => 'red'];

    /**
     * 重构日期行为配置
     * @return array
     */
    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['timestampBehavior'] = [
            'class' => DatetimeBehavior::class,
            'attributes' => [
                self::EVENT_BEFORE_INSERT => ['created_at'],
            ],
        ];

        return $behaviors;
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%app_logs}}';
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        $scenarios = parent::scenarios();

        // 场景 - 新增日志
        $scenarios['SCE_Log_Create'] = ['level', 'category', 'prefix', 'message', 'business_type', 'business_id'];

        return $scenarios;
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['level', 'category', 'prefix', 'message'], 'string'],
            [['created_at'], 'safe'],
            [['level', 'business_type'], 'string', 'max' => 20],
            [['category'], 'string', 'max' => 255],
            [['business_id'], 'integer'],
            [['level'], 'default', 'value' => 'DEBUG'],
            [['category', 'prefix', 'message', 'business_type'], 'default', 'value' => ''],
            [['business_id'], 'default', 'value' => 0],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'level' => '日志等级',
            'category' => '种类',
            'prefix' => '客户端',
            'message' => '信息',
            'business_type' => '业务类型',
            'business_id' => '业务ID',
            'created_at' => '记录时间',
        ];
    }
}
